/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.display;

import deatax.Terrain;
import deatax.Tile;
import javax.media.opengl.GL2;

/**
 *
 * @author Caroline
 */
public class DefaultTileRenderer extends TileRenderer {

    @Override
    public void draw(GL2 gl, Tile t, float px, float py, float rotation) {

        super.draw(gl, t, px, py, rotation);

        if (t.getTerrain() != null) {
            Terrain terrain = t.getTerrain();
            if (terrain.getTexture(t.getModelIndex()) != null) {
                gl.glEnable(GL2.GL_TEXTURE_2D);
                terrain.getTexture(t.getModelIndex()).bind(gl);
            }
//            if(t.getModelIndex() > 0){
//                System.out.println(t.getTerrain().getName() + ":" + t.getModelIndex());
//            }
            
            if (terrain.getModel(t.getModelIndex()) != null) {
                gl.glTranslatef(px, py, -0.34f);
                gl.glRotatef(rotation, 0, 0, 1);

                float scale = 4.5f;
                gl.glScalef(scale, scale, scale);
                terrain.getModel(t.getModelIndex()).draw(gl);
                scale = 1 / scale;
                gl.glScalef(scale, scale, scale);
                gl.glRotatef(rotation, 0, 0, -1);
                gl.glTranslatef(-px, -py, 0.34f);
            } else {
                gl.glBegin(GL2.GL_TRIANGLE_FAN);
                for (int i = 0; i < 12; i += 2) {
                    gl.glTexCoord2fv(hex_texture, i);
                    gl.glVertex3f(hex_inner[i] + px, hex_inner[i + 1] + py, TILE_BEVEL);
                }
                gl.glEnd();
            }

            if (terrain.getTexture() != null) {
                gl.glDisable(GL2.GL_TEXTURE_2D);
            }
        } else {
                gl.glColor3f(0.9f,0.9f,0.9f);
                gl.glBegin(GL2.GL_TRIANGLE_FAN);
                for (int i = 0; i < 12; i += 2) {
                    gl.glVertex3f(hex_inner[i] + px, hex_inner[i + 1] + py, TILE_BEVEL);
                }
                gl.glEnd();
            }

        
    }
    
}
